package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        this.save(student);
    }

    @Override
    public Student getStudent(Integer id) {
        Student student = this.getById(id);
        if (student != null) {
            if (student.getClazzId() != null) {
                Clazz clazz = clazzMapper.selectById(student.getClazzId());
                if (clazz != null) {
                    student.setClazzName(clazz.getName());
                }
            }
        }
        return student;
    }

    @Override
    public void violation(Integer id, Integer score) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getId, id);
        Student student = this.getOne(queryWrapper);
        if (student != null) {
            student.setViolationScore((short) (student.getViolationScore() + score));
            student.setViolationCount((short) (student.getViolationCount() + 1));
            student.setUpdateTime(LocalDateTime.now());
            this.update(student, queryWrapper);
        }
    }

    @Override
    public PageResult<Student> findList(StudentQueryParam student) {
        Page<Student> clazzPage = new Page<>(student.getPage(), student.getPageSize());
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<Student>()
                .like(!StringUtils.isEmpty(student.getName()), Student::getName, student.getName())
                .eq(!StringUtils.isEmpty(student.getDegree()), Student::getDegree, student.getDegree())
                .eq(!StringUtils.isEmpty(student.getClazzId()), Student::getClazzId, student.getClazzId());
        this.page(clazzPage, queryWrapper);
        clazzPage.getRecords().forEach(
                student1 -> {
                    if (student1.getClazzId() != null) {
                        Clazz clazz = clazzMapper.selectById(student1.getClazzId());
                        if (clazz != null) {
                            student1.setClazzName(clazz.getName());
                        }
                    }
                }
        );
        return new PageResult<>(clazzPage.getTotal(), clazzPage.getRecords());
    }
}
